package com.peza.postman.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service("propertyService")
public class PropertiesServiceImpl implements PropertyService{

	private Map<String,String> mailMap = new HashMap<String,String>();
	
	private String value;

	private String path;
	
	private String reloadInterval;
	
	private Map<String,String> properties;
	
	private static final String EQUALS = "=";

	private static final String COMMENT_PREFIX = "#";

	private static Logger logger = Logger.getLogger(PropertiesServiceImpl.class);
	
//	@Autowired
//	private ServletContext context;
	
	
	
	public String getProperty(String key) {
		String prop = null;
		if (mailMap != null) {
			if (key != null) {
				prop = mailMap.get(key);
			}
		}
		return prop;
	}

	public Map<String, String> getPropertyStartsWith(String startsWithKey) {
		Map<String, String> map = new HashMap<String, String>();
		if (mailMap != null) {
			if (startsWithKey != null) {
				for (Object key : mailMap.keySet()) {
					String keyValue = (String) key;
					if (keyValue.startsWith(startsWithKey)) {
						map.put(keyValue, getProperty(keyValue));
					}
				}
			}
		}
		return map;
	}

	public Map<String, String> getMailMap() {
		return mailMap;
	}

	public void setMailMap(Map<String, String> mailMap) {
		this.mailMap = mailMap;
	}

	
	@PostConstruct
	public void load() {
		
		File file = new File(path);
		//file = new File(context.getRealPath("/") + "WEB-INF/res/values.properties");
		try {
			if (properties == null) {
				createPropertiesFromFile(file);
			}

			if (reloadInterval != null) {
				long reloadMillis = Long.valueOf(reloadInterval);
				FileMonitor fileMonitor = FileMonitor.getInstance();
				fileMonitor.addFileChangeListener(new FileChangeListener() {

					public void fileChanged(String fileName) {
						try {
							createPropertiesFromFile(new File(fileName));
							logger.debug("PROPERTY SERVICE : properties reloaded");
						} catch (IOException e) {
							logger.error(e.getMessage(), e);
						}
					}
				}, file.getAbsolutePath(), reloadMillis);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	
	private void createPropertiesFromFile(File file) throws IOException {
		dir();
		properties = new HashMap<String,String>();
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = reader.readLine();
		while (line != null) {
			int equalsIndex = line.indexOf(EQUALS);
			if (equalsIndex != -1) {
				String key = line.substring(0, equalsIndex);
				if (!key.startsWith(COMMENT_PREFIX)) {
					String value = line.substring(equalsIndex + 1);
					properties.put(key, value);
				}
			}
			line = reader.readLine();
		}
	}

	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getReloadInterval() {
		return reloadInterval;
	}

	public void setReloadInterval(String reloadInterval) {
		this.reloadInterval = reloadInterval;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	private static void dir(){
		String currentDir = new File(".").getAbsolutePath();
		logger.info("DIR = " + currentDir);
	}

	public static void main (String [] args){
		PropertiesServiceImpl.dir();
	}

}
